﻿Imports System.Data.OleDb
Module empresa

    Public Class C_Empresa

        Public Function allEmpresas() As OleDbDataReader
            Using cadenaConexBD()
                Dim drEmpr As OleDbDataReader
                Dim instruccion As New OleDbCommand("SELECT * FROM empresa", cadenaConexBD)
                drEmpr = instruccion.ExecuteReader()
                Return drEmpr
                drEmpr.Close()
            End Using
        End Function

        Public Sub NuevaEmpresa()
            Try
                cadenaConexBD()
                Using cadenaConexBD()
                    Dim instruccion As New OleDbCommand("insert into empresa (nomb_empr , dire_emp , tel_empr, email_emp) values ('" & frm_empresa.txtnombEmp.Text & "','" & frm_empresa.txtdireEmp.Text & "','" & frm_empresa.txtTelEmp.Text & "','" & frm_empresa.txtemailempr.Text & "')", cadenaConexBD)
                    instruccion.ExecuteNonQuery()
                End Using
                MessageBox.Show("Nuevos datos agregados con éxito")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub



        Public Sub updateEmpresa()
            Try
                Using cadenaConexBD()
                    'id de la empresa a editar
                    Dim SearchEmprID As New OleDbCommand("SELECT id_empr FROM empresa WHERE nomb_empr = '" & frm_empresa.txtnombEmp.Text & "'", cadenaConexBD)
                    Dim id_Emp = SearchEmprID.ExecuteScalar
                    Dim UpdateEmp As New OleDbCommand("UPDATE empresa SET nomb_empr='" & frm_empresa.txtnombEmp.Text & "',dire_emp='" & frm_empresa.txtdireEmp.Text & "',tel_empr='" & frm_empresa.txtTelEmp.Text & "',email_emp='" & frm_empresa.txtemailempr.Text & "' where id_empr =" & id_Emp, cadenaConexBD)
                    UpdateEmp.ExecuteNonQuery()
                End Using
                MessageBox.Show("El registro ah sido modificado con exito", "Edicion de Registro", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub



    End Class

    Public Sub listadoEmpresas(ByVal flex As AxMSFlexGridLib.AxMSFlexGrid)
        Dim listEmpr As New C_Empresa
        Dim drResultEmpr As OleDbDataReader = listEmpr.allEmpresas

        Dim id_empr As Integer
        Dim nomb_empr As String = ""
        Dim dire_emp As String = ""
        Dim tel_empr As String = ""
        Dim email_emp As String = ""

        While drResultEmpr.Read

            If Not drResultEmpr.IsDBNull(0) Then
                id_empr = drResultEmpr.GetInt32(0)
            End If
            If Not drResultEmpr.IsDBNull(1) Then
                nomb_empr = drResultEmpr.GetString(1)
            End If
            If Not drResultEmpr.IsDBNull(2) Then
                dire_emp = drResultEmpr.GetString(2)
            End If
            If Not drResultEmpr.IsDBNull(3) Then
                tel_empr = drResultEmpr.GetString(3)
            End If
            If Not drResultEmpr.IsDBNull(4) Then
                email_emp = drResultEmpr.GetString(4)
            End If

            flex.AddItem(id_empr & Chr(9) & nomb_empr & Chr(9) & dire_emp & Chr(9) & tel_empr & Chr(9) & email_emp & Chr(9))
        End While
        drResultEmpr.Close()
    End Sub
    Public Sub TitulosGridEmpresa(ByVal flex As AxMSFlexGridLib.AxMSFlexGrid)
        With (flex)
            .Row = 0
            .Col = 0
            .set_ColWidth(0, 500)
            .Text = "ID"
            .Col = 1
            .set_ColWidth(1, 2000)
            .Text = "Nombre"
            .Col = 2
            .set_ColWidth(2, 3000)
            .Text = "Direccion"
            .Col = 3
            .set_ColWidth(3, 1500)
            .Text = "Teléfono"
            .Col = 4
            .set_ColWidth(4, 2200)
            .Text = "Email"
        End With

    End Sub

    Public Sub CargarEmpresas(ByVal lista As ComboBox)
        lista.Items.Clear()
        Dim empr As New C_Empresa
        Dim drallempr As OleDbDataReader = empr.allEmpresas
        If drallempr Is Nothing Then
            Exit Sub
        End If
        Dim strloc As String
        While drallempr.Read()
            strloc = ""
            If Not drallempr.IsDBNull(0) Then
                strloc = drallempr.GetString(1)
            End If
            lista.Items.Add(strloc)
        End While
        drallempr.Close()
    End Sub


End Module
